package com.hoho.android.usbserial.examples;

import android.graphics.Color;
import android.graphics.Matrix;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;
import java.util.List;

public class LineChartViewUtil {
    private LineChart lineChart;

    public LineChartViewUtil(LineChart paramLineChart) {
        this.lineChart = paramLineChart;
    }

    private LineDataSet createSet() {
        LineDataSet localLineDataSet = new LineDataSet(null, "");
        localLineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
        localLineDataSet.setColor(ColorTemplate.getHoloBlue());
        localLineDataSet.setLineWidth(2.0F);
        localLineDataSet.setFillAlpha(65);
        localLineDataSet.setFillColor(ColorTemplate.getHoloBlue());
        localLineDataSet.setHighLightColor(Color.rgb(244, 117, 117));
        localLineDataSet.setValueTextColor(-1);
        localLineDataSet.setValueTextSize(9.0F);
        localLineDataSet.setDrawValues(false);
        localLineDataSet.setDrawCubic(true);
        localLineDataSet.setDrawFilled(false);
        localLineDataSet.setDrawCircles(false);
        return localLineDataSet;
    }

    private LineDataSet createSet(List<Entry> paramList, String paramString, int paramInt) {
        LineDataSet localLineDataSet = new LineDataSet(paramList, paramString);
        localLineDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
        localLineDataSet.setColor(paramInt);
        localLineDataSet.setCircleColor(paramInt);
        localLineDataSet.setLineWidth(2.0F);
        localLineDataSet.setCircleSize(4.0F);
        localLineDataSet.setFillAlpha(65);
        localLineDataSet.setFillColor(paramInt);
        localLineDataSet.setDrawCircleHole(false);
        localLineDataSet.setHighLightColor(Color.rgb(244, 117, 117));
        localLineDataSet.setDrawValues(false);
        return localLineDataSet;
    }

    private List<String> getNumbers(int paramInt) {
        ArrayList localArrayList = new ArrayList();
        for (int i = 1; i <= paramInt; i++) {
            StringBuilder localStringBuilder = new StringBuilder();
            localStringBuilder.append(i);
            localStringBuilder.append("");
            localArrayList.add(localStringBuilder.toString());
        }
        return localArrayList;
    }

    public void addEntry(int paramInt) {
        LineData localLineData = (LineData) this.lineChart.getData();
        if (localLineData != null) {
            LineDataSet localLineDataSet = (LineDataSet) localLineData.getDataSetByIndex(0);
            if (localLineDataSet == null) {
                localLineDataSet = createSet();
                localLineData.addDataSet(localLineDataSet);
            }
            StringBuilder localStringBuilder = new StringBuilder();
            localStringBuilder.append("");
            localStringBuilder.append(localLineData.getXValCount());
            localLineData.addXValue(localStringBuilder.toString());
            localLineData.addEntry(new Entry(paramInt, localLineDataSet.getEntryCount()), 0);
            this.lineChart.notifyDataSetChanged();
            this.lineChart.setVisibleXRangeMaximum(280.0F);
            this.lineChart.moveViewToX(-281 + localLineData.getXValCount());
        }
    }

    public void addHRVEntry(int paramInt) {
        LineData localLineData = (LineData) this.lineChart.getData();
        if (localLineData != null) {
            LineDataSet localLineDataSet = (LineDataSet) localLineData.getDataSetByIndex(0);
            if (localLineDataSet == null) {
                localLineDataSet = createSet();
                localLineData.addDataSet(localLineDataSet);
                localLineDataSet.setDrawCubic(false);
            }
            StringBuilder localStringBuilder = new StringBuilder();
            localStringBuilder.append("");
            localStringBuilder.append(localLineData.getXValCount());
            localLineData.addXValue(localStringBuilder.toString());
            localLineData.addEntry(new Entry(paramInt, localLineDataSet.getEntryCount()), 0);
            this.lineChart.notifyDataSetChanged();
            this.lineChart.setVisibleXRangeMaximum(20.0F);
            this.lineChart.moveViewToX(-21 + localLineData.getXValCount());
        }
    }

    public void clearData() {
        LineData localLineData = new LineData();
        this.lineChart.setData(localLineData);
        this.lineChart.invalidate();
        this.lineChart.setScaleMinima(1.0F, 1.0F);
        this.lineChart.getViewPortHandler().refresh(new Matrix(), this.lineChart, true);
    }

    public LineData generateCheckReportDataLine(List<DetectionReport> paramList) {
        ArrayList localArrayList1 = new ArrayList();
        ArrayList localArrayList2 = new ArrayList();
        ArrayList localArrayList3 = new ArrayList();
        ArrayList localArrayList4 = new ArrayList();
        for (int i = 0; i < paramList.size(); i++) {
            DetectionReport localDetectionReport = (DetectionReport) paramList.get(i);
            localArrayList1.add(new Entry(localDetectionReport.getIndexPressure(), i));
            localArrayList2.add(new Entry(localDetectionReport.getIndexTired(), i));
            localArrayList3.add(new Entry(localDetectionReport.getIndexStabilization(), i));
            localArrayList4.add(new Entry(localDetectionReport.getIndexRelax(), i));
        }
        LineDataSet localLineDataSet1 = createSet(localArrayList1, "压力指数", -65536);
        LineDataSet localLineDataSet2 = createSet(localArrayList2, "疲劳指数", -16776961);
        LineDataSet localLineDataSet3 = createSet(localArrayList3, "稳定指数", -16711936);
        LineDataSet localLineDataSet4 = createSet(localArrayList4, "情绪指数", -256);
        ArrayList localArrayList5 = new ArrayList();
        localArrayList5.add(localLineDataSet1);
        localArrayList5.add(localLineDataSet2);
        localArrayList5.add(localLineDataSet3);
        localArrayList5.add(localLineDataSet4);
        return new LineData(getNumbers(paramList.size()), localArrayList5);
    }

    public LineData generateDataLine() {
        ArrayList localArrayList1 = new ArrayList();
        int i = 0;
        for (int j = 0; j < 20; j++)
            localArrayList1.add(new Entry(80 + (int) (10.0D * Math.random()), j));
        LineDataSet localLineDataSet1 = createSet(localArrayList1, "压力指数", -65536);
        ArrayList localArrayList2 = new ArrayList();
        for (int k = 0; k < 20; k++)
            localArrayList2.add(new Entry(((Entry) localArrayList1.get(k)).getVal() - 10.0F, k));
        LineDataSet localLineDataSet2 = createSet(localArrayList2, "疲劳指数", -16776961);
        ArrayList localArrayList3 = new ArrayList();
        for (int m = 0; m < 20; m++)
            localArrayList3.add(new Entry(((Entry) localArrayList1.get(m)).getVal() - 40.0F, m));
        LineDataSet localLineDataSet3 = createSet(localArrayList3, "稳定指数", -16711936);
        ArrayList localArrayList4 = new ArrayList();
        while (i < 20) {
            localArrayList4.add(new Entry(((Entry) localArrayList1.get(i)).getVal() - 50.0F, i));
            i++;
        }
        LineDataSet localLineDataSet4 = createSet(localArrayList4, "稳定指数", -256);
        ArrayList localArrayList5 = new ArrayList();
        localArrayList5.add(localLineDataSet1);
        localArrayList5.add(localLineDataSet2);
        localArrayList5.add(localLineDataSet3);
        localArrayList5.add(localLineDataSet4);
        return new LineData(getNumbers(20), localArrayList5);
    }

    public LineData generatePressureDataLine(List<DetectionReport> paramList) {
        ArrayList localArrayList1 = new ArrayList();
        for (int i = 0; i < paramList.size(); i++)
            localArrayList1.add(new Entry(((DetectionReport) paramList.get(i)).getIndexPressure(), i));
        LineDataSet localLineDataSet = createSet(localArrayList1, null, Color.rgb(0, 191, 255));
        ArrayList localArrayList2 = new ArrayList();
        localArrayList2.add(localLineDataSet);
        return new LineData(getNumbers(paramList.size()), localArrayList2);
    }

    public void initCheckReportChart(List<DetectionReport> paramList) {
        this.lineChart.setDescription("按次");
        this.lineChart.setHighlightEnabled(false);
        this.lineChart.setDrawGridBackground(false);
        this.lineChart.setScaleEnabled(false);
        this.lineChart.setTouchEnabled(true);
        this.lineChart.setDragEnabled(true);
        this.lineChart.setPinchZoom(true);
        this.lineChart.setNoDataText("");
        YAxis localYAxis = this.lineChart.getAxisLeft();
        localYAxis.setStartAtZero(false);
        localYAxis.setAxisMinValue(0.0F);
        localYAxis.setStartAtZero(false);
        localYAxis.setDrawGridLines(false);
        this.lineChart.getAxisRight().setEnabled(false);
        XAxis localXAxis = this.lineChart.getXAxis();
        localXAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        localXAxis.setDrawGridLines(false);
        localXAxis.setAvoidFirstLastClipping(true);
        LineData localLineData = generateCheckReportDataLine(paramList);
        this.lineChart.setData(localLineData);
        this.lineChart.setVisibleXRangeMaximum(9.0F);
        this.lineChart.moveViewToX(-10 + localLineData.getXValCount());
        this.lineChart.animateY(2000, Easing.EasingOption.EaseInCubic);
    }

    public void initLinChartHRV() {
        this.lineChart.setDescription("");
        this.lineChart.setHighlightEnabled(false);
        this.lineChart.setDrawGridBackground(false);
        this.lineChart.setScaleEnabled(false);
        this.lineChart.setTouchEnabled(false);
        this.lineChart.setDragEnabled(true);
        this.lineChart.setPinchZoom(true);
        this.lineChart.getLegend().setEnabled(false);
        this.lineChart.setNoDataText("");
        YAxis localYAxis = this.lineChart.getAxisLeft();
        localYAxis.setStartAtZero(false);
        localYAxis.setAxisMinValue(0.0F);
        localYAxis.setStartAtZero(false);
        localYAxis.setDrawGridLines(false);
        localYAxis.setDrawAxisLine(false);
        localYAxis.setTextColor(-7829368);
        this.lineChart.getAxisRight().setEnabled(false);
        XAxis localXAxis = this.lineChart.getXAxis();
        localXAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        localXAxis.setDrawGridLines(false);
        localXAxis.setAvoidFirstLastClipping(true);
        localXAxis.setSpaceBetweenLabels(5);
        localXAxis.setTextColor(-7829368);
        localXAxis.setAxisLineColor(-7829368);
        localXAxis.setDrawLabels(false);
        LineData localLineData = new LineData();
        this.lineChart.setData(localLineData);
    }

    public void initLinChartPulseWave() {
        this.lineChart.setDescription("");
        this.lineChart.setHighlightEnabled(false);
        this.lineChart.setDrawGridBackground(false);
        this.lineChart.setScaleEnabled(false);
        this.lineChart.setTouchEnabled(false);
        this.lineChart.setDragEnabled(true);
        this.lineChart.setPinchZoom(true);
        this.lineChart.getLegend().setEnabled(false);
        this.lineChart.setNoDataText("");
        YAxis localYAxis = this.lineChart.getAxisLeft();
        localYAxis.setStartAtZero(false);
        localYAxis.setAxisMinValue(200.0F);
        localYAxis.setStartAtZero(false);
        localYAxis.setDrawGridLines(false);
        localYAxis.setDrawAxisLine(false);
        localYAxis.setTextColor(-7829368);
        this.lineChart.getAxisRight().setEnabled(false);
        XAxis localXAxis = this.lineChart.getXAxis();
        localXAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        localXAxis.setDrawGridLines(false);
        localXAxis.setAvoidFirstLastClipping(true);
        localXAxis.setSpaceBetweenLabels(2);
        localXAxis.setTextColor(-7829368);
        localXAxis.setAxisLineColor(-7829368);
        localXAxis.setDrawLabels(false);
        LineData localLineData = new LineData();
        this.lineChart.setData(localLineData);
    }

    public void initLinChartSCL() {
        this.lineChart.setDescription("");
        this.lineChart.setHighlightEnabled(false);
        this.lineChart.setDrawGridBackground(false);
        this.lineChart.setScaleEnabled(false);
        this.lineChart.setTouchEnabled(false);
        this.lineChart.setDragEnabled(true);
        this.lineChart.setPinchZoom(true);
        this.lineChart.getLegend().setEnabled(false);
        this.lineChart.setNoDataText("");
        YAxis localYAxis = this.lineChart.getAxisLeft();
        localYAxis.setStartAtZero(false);
        localYAxis.setAxisMinValue(0.0F);
        localYAxis.setStartAtZero(false);
        localYAxis.setDrawGridLines(false);
        localYAxis.setDrawAxisLine(false);
        localYAxis.setTextColor(-7829368);
        this.lineChart.getAxisRight().setEnabled(false);
        XAxis localXAxis = this.lineChart.getXAxis();
        localXAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        localXAxis.setDrawGridLines(false);
        localXAxis.setAvoidFirstLastClipping(true);
        localXAxis.setSpaceBetweenLabels(5);
        localXAxis.setTextColor(-7829368);
        localXAxis.setAxisLineColor(-7829368);
        localXAxis.setDrawLabels(false);
        LineData localLineData = new LineData();
        this.lineChart.setData(localLineData);
    }

    public void initPressureIndexChart(List<DetectionReport> paramList) {
        this.lineChart.setDescription("");
        this.lineChart.setHighlightEnabled(false);
        this.lineChart.setDrawGridBackground(false);
        this.lineChart.setScaleEnabled(false);
        this.lineChart.setTouchEnabled(true);
        this.lineChart.setDragEnabled(true);
        this.lineChart.setPinchZoom(true);
        this.lineChart.getLegend().setEnabled(false);
        this.lineChart.setNoDataText("");
        YAxis localYAxis = this.lineChart.getAxisLeft();
        localYAxis.setStartAtZero(false);
        localYAxis.setAxisMinValue(0.0F);
        localYAxis.setStartAtZero(false);
        localYAxis.setDrawGridLines(false);
        localYAxis.setDrawAxisLine(false);
        localYAxis.setTextColor(-1);
        this.lineChart.getAxisRight().setEnabled(false);
        XAxis localXAxis = this.lineChart.getXAxis();
        localXAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        localXAxis.setDrawGridLines(false);
        localXAxis.setAvoidFirstLastClipping(true);
        localXAxis.setTextColor(-1);
        localXAxis.setAxisLineColor(-1);
        LineData localLineData = generatePressureDataLine(paramList);
        this.lineChart.setData(localLineData);
        this.lineChart.setVisibleXRangeMaximum(9.0F);
        this.lineChart.animateX(2500);
        this.lineChart.moveViewToX(-10 + localLineData.getXValCount());
    }
}